package com.example.utils;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.example.entities.MP3;

import java.util.ArrayList;
import java.util.List;

public class MusicUtils {
    public static String timeParse(int duration){//将音乐的时长变为时间字符串
        String str="";
        int minute=duration/60000;
        int seconds=duration/1000%60;
        if(minute<10){
            str="0"+minute;
        }else{
            str=""+minute;
        }
        if(seconds<10){
            str+=":0"+seconds;
        }else{
            str+=":"+seconds;
        }
        return str;
    }

    public static List<MP3> loadLocalMP3(Context context){//获取Music数据
        List<MP3> data=new ArrayList<>();
        int id=0;//编号
        //获取数据
        Cursor cursor=context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.AudioColumns.IS_MUSIC);
        if(cursor.moveToFirst()){
            do{
                int duration=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                int size=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
                String title =cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                String musician=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                String url=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                MP3 mp3=new MP3(id,duration,size,url,title,musician);

                data.add(mp3);
                id++;
            }while(cursor.moveToNext());
        }
        cursor.close();
        return data;
    }
}
